//
//  RGMessageFuncItem.m
//  RedGrass
//
//  Created by wukong on 2018/11/25.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMessageFuncItem.h"

@interface RGMessageFuncItem()

@property (strong, nonatomic) UIImageView *icon;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *actionBtn;

@end

@implementation RGMessageFuncItem

+(RGMessageFuncItem *)initRGMessageFuncItemWithIcon:(NSString *)icon title:(NSString *)title{
    RGMessageFuncItem *item = [[RGMessageFuncItem alloc]initWithFrame:CGRectZero];
    item.icon.image = IMAGE_NAMED(icon);
    item.titleLabel.text = title;
    return item;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.icon];
        [self addSubview:self.titleLabel];
        [self addSubview:self.actionBtn];
        [self addMasonry];
    }
    return self;
}

-(void)addMasonry{
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerX.mas_equalTo(self).mas_offset(0);
        make.top.mas_equalTo(self).mas_offset(8);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self).mas_offset(0);
        make.top.mas_equalTo(self.icon.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(self).mas_offset(3);
    }];
}


#pragma mark - response
-(void)actionBtnClicK:(UIButton *)button{
    if(self.actionBtnClick){
        self.actionBtnClick(self.tag);
    }
}

#pragma mark - getter
-(UIImageView *)icon{
    if(!_icon){
        _icon = [[UIImageView alloc]init];
        _icon.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionBtnClicK:)];
        [_icon addGestureRecognizer:tap];
    }
    return _icon;
}

-(UILabel *) titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = FONT(customFontName, 14);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_actionBtn addTarget:self action:@selector(actionBtnClicK:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}


@end
